//
//  JXHomeController.m
//  JiaoX
//
//  Created by jiaoxiu－004 on 14-10-11.
//  Copyright (c) 2014年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXOldHomeController.h"
#import "HomeDataTool.h"
#import "HomeDataInfo.h"
#import "SubjectItemInfo.h"
#import "ProductItemInfo.h"
#import "SubjectViewFrame.h"
#import "JXBaseResponse.h"
#import "SubjectItemCell.h"
#import "MJRefresh.h"
#import "HomeProdItemCell.h"
#import "HomeProdViewFrame.h"
#import "HomeProdTitleView.h"

@interface JXOldHomeController ()<UITableViewDataSource,UITableViewDelegate>
{
    HomeProdTitleView *prodTitleView;
}

@property(nonatomic,weak)UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *allFrames;

@end

@implementation JXOldHomeController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化两个集合
    _allFrames = [NSMutableArray array];
    UITableView *tableVeiw = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
//    tableVeiw.backgroundColor = JXGlobalColorBg;
    tableVeiw.sectionFooterHeight = 10;
    
    //设置一个headerview和footerview，以防止当tableview指定UITableViewStyleGrouped这个style的时候，顶部和底部都会多那么一截
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, JXScreenW, 0.01)];
    view.backgroundColor = [UIColor clearColor];
    tableVeiw.tableHeaderView = view;
    tableVeiw.tableFooterView = view;
    
    [self.view addSubview:tableVeiw];
    
    _tableView = tableVeiw;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    self.navigationItem.title = @"娇羞";
    //去掉tableview的分割线
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    //集成下拉刷新与上拉加载
    [self.tableView addHeaderWithTarget:self action:@selector(requestData)];
    [self.tableView addFooterWithTarget:self action:@selector(requestData)];
    
    [self.tableView headerBeginRefreshing];
}



-(void)requestData{
    JXBaseRequest *request = [[JXBaseRequest alloc]init];
    [HomeDataTool homeOldDataInfoWithParam:[request keyValues] success:^(HomeDataInfo *result) {
        
        // 获得最新数据的frame数组
        [self homeDataFramesWithDatas:result];

        [self.tableView reloadData];
        [self refreshComplete];
    } failure:^(NSError *error) {
        [self refreshComplete];
    }];
}

-(void)refreshComplete{
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
}


/**
 *  根据首页模型数组 转成 微博frame模型数据
 *
 *  @param homeDataInfo
 *
 */
- (void)homeDataFramesWithDatas:(HomeDataInfo *)homeDataInfo
{
    
    [self.allFrames removeAllObjects];
    NSMutableArray *subjectFrames = [NSMutableArray array];
    for (SubjectItemInfo *subject in homeDataInfo.subjectList) {
        SubjectViewFrame *frame = [[SubjectViewFrame alloc]init];
        //传递专题数据，计算所有子控件的frame
        frame.subject = subject;
        [subjectFrames addObject:frame];
    }
    [self.allFrames addObject:subjectFrames];
    
    
    
    NSMutableArray *productFrames = [NSMutableArray array];
    for (ProductItemInfo *prod in homeDataInfo.productList) {
        HomeProdViewFrame *frame = [[HomeProdViewFrame alloc]init];
        frame.product = prod;
        [productFrames addObject:frame];
    }
    
    [self.allFrames addObject:productFrames];
    
}

#pragma mark - Table view 数据源

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.allFrames.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *subitems = _allFrames[section];
    return subitems.count;
}


/**
 *  返回对应位置的cell
 *
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section==0){
        SubjectItemCell *cell = [SubjectItemCell cellWithTableView:tableView];
        cell.subjectFrame = self.allFrames[indexPath.section][indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        HomeProdItemCell *cell = [HomeProdItemCell cellWithTableView:tableView];
        cell.homeProdFrame = self.allFrames[indexPath.section][indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        return cell;
    }
    
}

/**
 *  返回组的头view
 *
 */
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if(section!=0){
        NSArray *prodArray = [_allFrames lastObject];
        HomeProdViewFrame *prodFrame = [prodArray firstObject];
        if(prodFrame){
            prodTitleView = [[HomeProdTitleView alloc]initWithIconUrl:prodFrame.product.prodTiltleIcon Title:prodFrame.product.prodTitleName];
            tableView.sectionHeaderHeight = prodTitleView.height;
            return prodTitleView;
            
        }
    }
    return nil;
}

/**
 *  返回组的头高度
 *
 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section==1){//如果首页数据里面有商品，并且商品要显示标题，那么返回30
        NSArray *prodArray = [_allFrames lastObject];
        HomeProdViewFrame *prodFrame = [prodArray firstObject];
        if(prodFrame && ![Utils isBlankString:prodFrame.product.prodTitleName]){
            return 30;
        }
    }
    return 0;
}

#pragma mark - Table view 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0){
        SubjectViewFrame *frame = self.allFrames[indexPath.section][indexPath.row];
        return frame.cellHeight;
    }else{
        HomeProdViewFrame *frame = self.allFrames[indexPath.section][indexPath.row];
        return frame.cellHeight;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        SubjectViewFrame *frame = self.allFrames[indexPath.section][indexPath.row];
        SubjectItemInfo *subInfo = frame.subject;
        [Utils dealUrlWithController:self url:subInfo.subUrl title:subInfo.cardName fromWebView:false];
    }else{
        
    }
}

//-(void)viewWillDisappear:(BOOL)animated{
//    if(_allFrames.count==0){
//        //重新请求数据
//        [self requestData];
//    }
//}

@end
